package libaryinterface.admin;
import libaryinterface.interfaceutils.IsDeleteInterface;
import libaryinterface.interfaceutils.MyDialogDemo;
import pojo.vo.BookVo;
import service.BookRecordService;
import service.BookService;
import service.UserService;
import service.impl.BookRecordServiceImpl;
import service.impl.BookServiceImpl;
import service.impl.UserServiceImpl;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-27
 * Time: 8:39
 * author:LendBookInterface
 */
//管理员显示借阅记录界面
public class AdminDispalyInterface extends JFrame {
    int i = 0;
    BookRecordService bookRecordService = new BookRecordServiceImpl();
    UserService userService = new UserServiceImpl();
    public AdminDispalyInterface() throws SQLException {
        //设置窗口
        super("管理员显示界面");
        Container container = this.getContentPane();

        ImageIcon bg = new ImageIcon("src//picture//admindisplay.jpg");
        JLabel label2 = new JLabel(bg);
        label2.setSize(bg.getIconWidth(),bg.getIconHeight());
        AdminDispalyInterface.super.getLayeredPane().add(label2,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)AdminDispalyInterface.super.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));
        JButton jButton4=new JButton("刷新");
        jButton4.setBounds(100,850,100,60);
        container.add(jButton4);
        JButton jButton5=new JButton("goback");
        jButton5.setBounds(30,30,100,30);
        container.add(jButton5);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(180, 1, 3000, 1200);
        //查询输入框
        JTextField find1 = new JTextField(60);
        find1.setBounds(450, 100, 500, 50);
        find1.setFont(new Font(null, Font.PLAIN, 20));
        container.add(find1);
        //查询按钮
        JButton jButton = new JButton("查询");
        jButton.setBounds(1000, 100, 150, 50);
        jButton.setFont(new Font("黑体", Font.PLAIN, 15));
        container.add(jButton);
        //查询按钮
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {

                    new AdminFindInterface(find1.getText());
                    AdminDispalyInterface.super.dispose();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        //设置标签
        JLabel label = new JLabel("选中要借阅的书,点击\"借阅\"按钮可借阅: ");
        label.setBounds(100, 100, 500, 40);
        label.setFont(new Font(null, Font.PLAIN, 20));
        label.setForeground(Color.GREEN);
        container.add(label);
        // 表头（列名）
        setBounds(200, 200, 1500, 1200);
        // 表头（列名）
        Object[][] rowData = new Object[100][8];
        BookService bookService = new BookServiceImpl();
        List<BookVo> bookList = bookService.listBook();
        //记录当前的条数
        String[] columnNames = {" SNID", "书名", "作者", " 出版社 ", "剩余数量", "借出数量", "总数量", "存放地点"};
        for (BookVo book : bookList) {
            //获取信息
            Long snid = book.getSnid();
            String name = book.getName();
            String author = book.getAuthor();
            String press = book.getPress();
            Long surplusNumber = book.getSurplusNumber();
            Long lendNumber = book.getLendNumber();
            Long allNumber = book.getAllNumber();
            String storagePlace = book.getStoragePlace();
            //放入表格数组中
            rowData[i][0] = snid;
            rowData[i][1] = name;
            rowData[i][2] = author;
            rowData[i][3] = press;
            rowData[i][4] = surplusNumber;
            rowData[i][5] = lendNumber;
            rowData[i][6] = allNumber;
            rowData[i][7] = storagePlace;
            i++;
        }

        // 创建一个表格，指定 表头 和 所有行数据
        JTable table = new JTable(rowData, columnNames);

        // 设置表格内容颜色
        table.setForeground(Color.BLACK);                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(Color.DARK_GRAY);      // 选中后字体颜色
        table.setSelectionBackground(Color.YELLOW);     // 选中后字体背景
        table.setGridColor(Color.GRAY);                     // 网格颜色
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        // 设置表头
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));  // 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.RED);                // 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(true);               // 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);             // 设置不允许拖动重新排序各列

        // 设置行高
        table.setRowHeight(40);

        // 第一列列宽设置为40
        table.getColumnModel().getColumn(0).setPreferredWidth(40);

        // 设置滚动面板视口大小（超过该大小的行数据，需要拖动滚动条才能看到）
        table.setPreferredScrollableViewportSize(new Dimension(400, 300));

        // 把 表格 放到 滚动面板 中（表头将自动添加到滚动面板顶部）
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(100, 200, 1100, 600);
        // 添加 滚动面板 到 内容面板
        container.add(scrollPane);
        Font font = new Font("黑体", Font.BOLD, 20);

        //创建修改按钮
        JButton button1 = new JButton("修改");
        button1.setBounds(450, 850, 100, 40);
        button1.setFont(font);
        container.add(button1);
        //设置修改监听
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取当前的行列数
                int index = 0;
                try {
                    index = table.getSelectedRow();
                    //判断选中行是否有信息
                    if( index >= i) {
                        new MyDialogDemo("请选中有信息的行!");
                    } else {
                        new AdminUpdateInterface(rowData,index);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    new MyDialogDemo("未选中行!");
                }
            }
        });

        //添加按钮
        JButton jButton2 = new JButton("添加");
        jButton2.setBounds(650, 850, 100, 40);
        jButton2.setFont(font);
        container.add(jButton2);
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new AdminAddBookInterface();
                } catch (Exception ex){
                    System.out.println("添加失败");
                    ex.printStackTrace();
                }
            }
        });

        //删除按钮
        JButton jButton3 = new JButton("删除");
        jButton3.setBounds(850, 850, 100, 40);
        jButton3.setFont(font);
        container.add(jButton3);

        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取当前的行列数
                int index = 0;
                try {
                    index = table.getSelectedRow();
                    //判断选中行是否有信息
                    if( index >= i) {
                        new MyDialogDemo("请选中有信息的行!");
                    } else {
                        Long BookId = (Long) rowData[index][0];
                        new IsDeleteInterface(BookId);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    new MyDialogDemo("未选中行!");
                }

            }
        });
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminDispalyInterface.super.dispose();
                try {
                    new AdminDispalyInterface();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AdminInterface();
                AdminDispalyInterface.super.dispose();
            }
        });



        //获得容器
        container.setBackground(new Color(50, 144, 236));
        setBounds(180, 1, 1300, 1200);
        container.setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
